package com.android.Translator.View;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;

import com.android.Translator.R;
import com.android.Translator.Presenter.TranslatorPresenter;
import com.android.Translator.View.utils.ViewUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class SelectTimeActivity extends Activity{
	private HashMap<String, Integer> m_TimeList = new HashMap<String, Integer>();
	private String m_SpinnerItemValue;
	private Spinner m_Spinner, m_SpinnerGroup;
	private Button m_Button;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_time_layout);
		initTimeList();
		initView();
		SetArrayListToSpinner(m_Spinner, getArrayListFromHashMap(m_TimeList));
	}
	
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		ArrayList<String> groupNameList = TranslatorPresenter.getGroupList(this);
		SetArrayListToSpinner(m_SpinnerGroup, groupNameList);
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	private void SetArrayListToSpinner(Spinner spinner, ArrayList<String> list)
	{
		ArrayAdapter<String> apt = new ArrayAdapter<String>(this, R.layout.spinner_custom_layout, 
														R.id.spinner_text, list);
		spinner.setAdapter(apt);
	}
	private void initView() {
		// TODO Auto-generated method stub
		m_Spinner = (Spinner) findViewById(R.id.TIME_SPINNER);
		m_SpinnerGroup = (Spinner) findViewById(R.id.spinner_Choose_Group);
		m_Button = (Button) findViewById(R.id.TIME_BUTTON_OK);
		m_Spinner.setSelection(0);
		m_Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View arg1,
					int Positon, long arg3) {
				// TODO Auto-generated method stub
				m_SpinnerItemValue = parent.getItemAtPosition(Positon).toString();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
			}
		});
		m_Button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String selectedGroup =  "";
				ArrayList<String> tmp = null;
				if (m_SpinnerGroup.getSelectedItem() != null)
				{
					selectedGroup = m_SpinnerGroup.getSelectedItem().toString();
					tmp = TranslatorPresenter.getMyWords(SelectTimeActivity.this, selectedGroup);
				}
				if (tmp != null && tmp.size()>0)
				{
					Bundle b = new Bundle();
					b.putInt("RATE_TIME", m_TimeList.get(m_SpinnerItemValue));
					b.putString("GROUPNAME",selectedGroup);
					Intent i = new Intent(getApplicationContext(), LearnNewWord.class);
					i.putExtra("TIME_PACKAGE", b);
					startActivity(i);
					finish();
				}
				else
				{
					Activity AddWordAct = new AddWordToLearn();;
					ViewUtils.ShowAlertDialog(SelectTimeActivity.this, AddWordAct , ViewUtils.ACTION_LAUNCH, 	
																			getString(R.string.TITLE_ALERT_DIALOG), 
																			getString(R.string.MESSAGE_DONT_HAVE_WORD));
				}
			}
		});
	}
	private void initTimeList() {
		// TODO Auto-generated method stub
		m_TimeList.put("10s", new Integer(ViewUtils.TIME_10_SECONDS));
		m_TimeList.put("20s", new Integer(ViewUtils.TIME_20_SECONDS));
		m_TimeList.put("30s", new Integer(ViewUtils.TIME_30_SECONDS));
	}
	
	private ArrayList<String> getArrayListFromHashMap(HashMap<String, Integer> map)
	{
		ArrayList<String> tmp = new ArrayList<String>();
		for (Entry<String, Integer> entry : map.entrySet())
		{
			Log.i("Translator", "XXXXXXXXXXX  "+ entry.getKey());
			tmp.add(entry.getKey());
		}
		return tmp;
	}
}
